package com.tiantian.quanminxiu.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.utils.APPUtils;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Created by admin on 2016/1/14.
 */
public class RankingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
    private int Type;       //type=1;是前三面 ， type = 2是普通
    private Activity activity;
    private JSONArray list;
    public RankingAdapter(Activity activity, JSONArray list){
        this.activity = activity;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        RecyclerView.ViewHolder holder = null;
        switch (viewType){
            case 1:
                holder = new MyViewHolder(LayoutInflater.from(
                        activity).inflate(R.layout.main_ranking_item2, parent,
                        false));
                break;
            case 2:
                holder = new MyViewHolder2(LayoutInflater.from(
                        activity).inflate(R.layout.main_ranking_item, parent,
                        false));
                break;
        }

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        int type = getItemViewType(position);
        if(type==1){
            MyViewHolder holder1 = (MyViewHolder) holder;
            JSONObject user = list.optJSONObject(position);
            switch (position){
                case 0:
                    holder1.imgUserRanking.setImageResource(R.drawable.icon_ranking1);
                    break;
                case 1:
                    holder1.imgUserRanking.setImageResource(R.drawable.icon_ranking2);
                    break;
                case 2:
                    holder1.imgUserRanking.setImageResource(R.drawable.icon_ranking3);
                    break;
            }
            holder1.txtUserName.setText(user.optString("NickName"));
            holder1.imgUserAvatar.post(new Runnable() {
                @Override
                public void run() {
                    APPUtils.displayImage(APPUtils.createUserAvatar(user.optInt("User_ID")), holder1.imgUserAvatar, activity.getBaseContext(), holder1.imgUserAvatar.getHeight() / 2);
                }
            });
            holder1.vMain.setTag(user);
            RxView.clicks(holder1.vMain).map(View->holder1.vMain).subscribe(v -> {
                JSONObject u = (JSONObject) v.getTag();
                APPUtils.Dialog_UserInfoCard_To(activity, u.optInt("User_ID"), u.optString("NickName"), -1, u.optJSONObject("Level").optInt("Level"),u.optInt("Anchor_Level"), false, false);
            });
            holder1.imgUserLevel.setImageResource(APPUtils.getLevelPic(activity, user.optJSONObject("Level").optInt("Level")));
            holder1.imgAnchorLevel.setImageResource(APPUtils.getAnchorLevelPic(activity,user.optInt("Anchor_Level")));
        }else {
            MyViewHolder2 holder2 = (MyViewHolder2) holder;
            JSONObject user = list.optJSONObject(position);
            holder2.txtUserRanking.setText((position+1)+"");
            holder2.txtUserName.setText(user.optString("NickName"));
            holder2.imgUserAvatar.post(new Runnable() {
                @Override
                public void run() {
                    APPUtils.displayImage(APPUtils.createUserAvatar(user.optInt("User_ID")), holder2.imgUserAvatar, activity.getBaseContext(), holder2.imgUserAvatar.getHeight() / 2);
                }
            });
            holder2.vMain.setTag(user);
            RxView.clicks(holder2.vMain).map(View->holder2.vMain).subscribe(v -> {
                JSONObject u = (JSONObject) v.getTag();
                APPUtils.Dialog_UserInfoCard_To(activity, u.optInt("User_ID"), u.optString("NickName"), -1, u.optJSONObject("Level").optInt("Level"),u.optInt("Anchor_Level"), false, false);
            });
            holder2.imgUserLevel.setImageResource(APPUtils.getLevelPic(activity, user.optJSONObject("Level").optInt("Level")));
            holder2.imgAnchorLevel.setImageResource(APPUtils.getAnchorLevelPic(activity, user.optInt("Anchor_Level")));
        }
    }

    @Override
    public int getItemCount()
    {
        return list==null?0: list.length();
    }

    @Override
    public int getItemViewType(int position) {
        if(position<3)
            return 1;
        return 2;
    }


    class MyViewHolder extends RecyclerView.ViewHolder
    {
        TextView txtUserName;
        ImageView imgUserAvatar,imgUserRanking,imgUserLevel,imgAnchorLevel;
        View vMain;

        public MyViewHolder(View view)
        {
            super(view);
            vMain = view.findViewById(R.id.vMain);
            imgUserAvatar = (ImageView) view.findViewById(R.id.imgUserAvatar);
            imgUserRanking = (ImageView) view.findViewById(R.id.imgUserRanking);
            imgUserLevel = (ImageView) view.findViewById(R.id.imgUserLevel);
            imgAnchorLevel = (ImageView) view.findViewById(R.id.imgAnchorLevel);
            txtUserName = (TextView) view.findViewById(R.id.txtUserName);
        }
    }


    class MyViewHolder2 extends RecyclerView.ViewHolder
    {
        TextView txtUserRanking,txtUserName;
        ImageView imgUserAvatar,imgUserLevel,imgAnchorLevel;
        View vMain;

        public MyViewHolder2(View view)
        {
            super(view);
            vMain = view.findViewById(R.id.vMain);
            imgUserLevel = (ImageView) view.findViewById(R.id.imgUserLevel);
            imgAnchorLevel = (ImageView) view.findViewById(R.id.imgAnchorLevel);
            imgUserAvatar = (ImageView) view.findViewById(R.id.imgUserAvatar);
            txtUserRanking = (TextView) view.findViewById(R.id.txtUserRanking);
            txtUserName = (TextView) view.findViewById(R.id.txtUserName);
        }
    }
}
